﻿<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="my.myorm.tool.jdbc.SQLExecutor" %>
<%@ page import="my.myorm.tool.jdbc.SQLManager" %>
<%@ page import="java.util.Date" %>
<%@ page import="my.myorm.tool.jdbc.ResultList" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.util.SuperPage" %>
<%@ page import="com.mtbu.comm.Util" %>
<%@ page import="com.mtbu.dao.DAOManager" %>
<%@ page import="com.mtbu.service.LogUtil" %>
<%@ page import="com.mtbu.bean.*"%>
<%@ page import="com.util.SuperDate"%>
<%@ page import="java.io.File"%>
<%@ page import="my.tool.ConfigProp" %>
<%@ page import="com.mtbu.comm.*"%>
<%@ page import="my.mypool.core.memcache.*"%>

<%@ include file="sys_commcheck.jsp" %>
<%
    //System.out.println("request:" + request.getRequestURI());
    //System.out.println("request:" + request.getRequest());

    String act = SuperString.notNullTrim(request.getParameter("act"));





    String curFile = SuperPage.getCurURLFile(request);
    String backFile = SuperString.replace(curFile, "moddo", "");
    String curName = "Channel";


    String pid = SuperString.notNullTrim(request.getParameter("pid"));
    String ids = SuperString.notNullTrim(request.getParameter("ids"));


    if( ids.isEmpty() ) {
        Util.callInfo("错误的调用方法！", request, response);
        return;
    }

    SuperDate sdnow = new SuperDate();
    Channel record = null;

	//先处理ids
	String idArr[] = ids.split(",");
	System.out.println("idArr:" + java.util.Arrays.toString(idArr));
	boolean hasError = false;
	String errMsg = "";
	Project project = DAOManager.projectDao.findProject(pid);
    if(act.equalsIgnoreCase("del")) {

		for (String id : idArr) {
		//	Channel channel = new Channel();
		//	channel.setId(SuperString.getInt(id));
			record = DAOManager.channelDao.findChannel("id",id,project.getSplit());
			if(record == null){
				errMsg += "删除失败，没有找到该id的记录!";
			}
			else{
				try{
					DAOManager.channelDao.delete(record,project.getSplit());
				}
				catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
					errMsg += "删除失败，破坏参照完整性";
				}
				catch(Exception e){
					errMsg += "删除失败" + e.getMessage();
				}
			}
		}

		LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
				   ids , request, null);


    }

	if(act.equalsIgnoreCase("stepnext")) {

		for (String id : idArr) {
		//	Channel channel = new Channel();
		//	channel.setId(SuperString.getInt(id));
			record = DAOManager.channelDao.findChannel("id",id,project.getSplit());
			if(record == null){
				errMsg += "没有找到该id的记录!";
			}
			else{
				try{
					Step step = record.getStep();
					if(step.doWork()){
						errMsg += "执行失败";
					}
					else{
						record.setStepId(step.getNextStep().getId());
						DAOManager.channelDao.update(record,project.getSplit());
					}
				}
				catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
					errMsg += "删除失败，破坏参照完整性";
				}
				catch(Exception e){
					errMsg += "失败" + e.getMessage();
				}
			}
		}
		LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
				   ids , request, null);

	}

	if(act.equalsIgnoreCase("stepback")) {

		for (String id : idArr) {
		//	Channel channel = new Channel();
		//	channel.setId(SuperString.getInt(id));
			record = DAOManager.channelDao.findChannel("id",id,project.getSplit());
			if(record == null){
				errMsg += "没有找到该id的记录!";
			}
			else{
				try{

					Step step = record.getStep();

					if(step.getPreStep().doRollBack()){
						errMsg += "执行失败";
					}
					else{

						record.setStepId(step.getPreStep().getId());
						DAOManager.channelDao.update(record,project.getSplit());
					}
				}
				catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
					errMsg += "删除失败，破坏参照完整性";
				}
				catch(Exception e){
					errMsg += "失败" + e.getMessage();
				}
			}
		}
		LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
				   ids , request, null);

	}

	if(errMsg.isEmpty()==false ){
		Util.callInfo(errMsg,request, response);
		return;
	}

    response.sendRedirect(backFile + SuperPage.getQueryQ(request, "act,ids"));
    return;
%>